package com.jstyle.jclife.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jstyle.jclife.R;
import com.jstyle.jclife.adapter.DeviceListAdapter;
import com.jstyle.jclife.model.BleData;

import com.jstyle.jclife.model.Device1790;

import com.jstyle.jclife.model.JstyleDevice;
import com.jstyle.jclife.utils.RxBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class DeviceTypeActivity extends BaseActivity implements DeviceListAdapter.onItemClickListener {

    @BindView(R.id.bt_device_title)
    Button btDeviceTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.RecyclerView_deviceType)
    RecyclerView RecyclerViewDeviceType;
    private DeviceListAdapter deviceListAdapter;
    private Disposable subscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_type);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        RecyclerViewDeviceType.setLayoutManager(linearLayoutManager);
        List<JstyleDevice> list=new ArrayList<>();
        list.add(new Device1790());

        deviceListAdapter=new DeviceListAdapter(list,this);
        RecyclerViewDeviceType.setAdapter(deviceListAdapter);
        subscription = RxBus.getInstance().toObservable(BleData.class).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<BleData>() {
            @Override
            public void accept(BleData bleData) throws Exception {
                String action = bleData.getAction();
                if (BindDeviceActivity.BindFinishAction.equals(action)) {
                        finish();
                }
            }});
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unSubscribe(subscription);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
    public static final String deviceType="deviceType";
    @Override
    public void onItemClick(JstyleDevice jstyleDevice) {
        Intent intent=new Intent(this,BindDeviceActivity.class);
        intent.putExtra(deviceType,jstyleDevice);
        startActivity(intent);
    }
}
